package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Privrecord;
import com.biostime.qdingding.modules.qd.service.PrivrecordService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 特权纪录Controller
 * @author phil
 * @version 2015-12-16
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/privrecord")
public class PrivrecordController extends BaseController {

	@Autowired
	private PrivrecordService privrecordService;
	
	@ModelAttribute
	public Privrecord get(@RequestParam(required=false) String id) {
		Privrecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = privrecordService.get(id);
		}
		if (entity == null){
			entity = new Privrecord();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:privrecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(Privrecord privrecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Privrecord> page = privrecordService.findPage(new Page<Privrecord>(request, response), privrecord); 
		model.addAttribute("page", page);
		return "modules/qd/privrecordList";
	}

	@RequiresPermissions("qd:privrecord:view")
	@RequestMapping(value = "form")
	public String form(Privrecord privrecord, Model model) {
		model.addAttribute("privrecord", privrecord);
		return "modules/qd/privrecordForm";
	}

	@RequiresPermissions(value={"qd:privrecord:edit","qd:privrecord:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Privrecord privrecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, privrecord)){
			return form(privrecord, model);
		}
		
		User curUser = UserUtils.getUser();
		privrecord.setCenter(curUser.getCenter());
		
		privrecordService.save(privrecord);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/privrecord/?repage";
	}
	
	@RequiresPermissions("qd:privrecord:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Privrecord privrecord, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				privrecordService.delete(new Privrecord(id));
			}
		}else{
			privrecordService.delete(privrecord);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/privrecord/?repage";
	}

}